package com.vonchenchen.android_renderer;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.vonchenchen.test.SystemHelper;

/**
 * Created by  on 2021/2/15.
 */
public class TestActivity extends Activity {

    private static final String TAG = "TestActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_test);

        findViewById(R.id.btn_test).setOnClickListener((v)->{

            Intent intent = new Intent();
            intent.setAction("com.vonchenchen.renderer");
            sendBroadcast(intent);
        });

        initReceiver();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        deinitReceiver();
    }

    private InternalReceiver mInternalReceiver;
    private boolean mIsForground;

    @Override
    protected void onResume() {
        super.onResume();
        mIsForground = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        mIsForground = false;
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {

            if(mIsForground){
                moveTaskToBack(true);
                Log.i(TAG, "back...");
            }else {
                SystemHelper.setTopApp(TestActivity.this);
                Log.i(TAG, "forgound...");
            }

            if (mHandler != null) {
                Message reMsg = new Message();
                reMsg.what = msg.what;
                reMsg.arg1 = msg.arg1;
                mHandler.sendMessageDelayed(reMsg, reMsg.arg1);
            }
        }
    };

    private static final String BROAD_CAST_BACK = "com.vonchenchen.renderer";

    void initReceiver(){

        if(mInternalReceiver == null){
            IntentFilter intentfilter = new IntentFilter();
            intentfilter.addAction(BROAD_CAST_BACK);
            mInternalReceiver = new InternalReceiver();
            registerReceiver(mInternalReceiver, intentfilter);
        }
    }

    void deinitReceiver(){
        if(mInternalReceiver != null) {
            unregisterReceiver(mInternalReceiver);
            mInternalReceiver = null;
        }
    }

    private class InternalReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            if (BROAD_CAST_BACK.equals(intent.getAction())){
                mHandler.removeMessages(1);
                Message msg = new Message();
                msg.arg1 = 1000;
                msg.what = 1;
                mHandler.sendMessageDelayed(msg, 1000);
            }
        }
    }

    //bruteforce test renderer
    //adb shell am broadcast -a com.vonchenchen.renderer
}
